import * as vscode from 'vscode';
import trie from "./trie";

export function activate(context: vscode.ExtensionContext) {
	const language = vscode.languages.registerCompletionItemProvider('bkm', {
		provideCompletionItems(document, position, token, context) {
			const completionItems = [];
			let linePrefix = document.lineAt(position).text.substring(0, position.character)
			let mode = 1;
			if (linePrefix.indexOf(",") !== -1) {
				mode = 2;
				linePrefix = linePrefix.substring(linePrefix.lastIndexOf(",")+1, position.character)
			}
			
			const items = trie.search(linePrefix);
			if (items.length > 0) {
				for (const value of items) {
					if (mode == 2 && value.mode !== mode) {
						continue;
					}
					const snippetCompletion = new vscode.CompletionItem(value.name, vscode.CompletionItemKind.Text);
					snippetCompletion.insertText = new vscode.SnippetString(value.insert);
					snippetCompletion.detail = value.detail;
					snippetCompletion.filterText = `${value.name} ${value.tags.join(" ")}`;
					snippetCompletion.documentation = value.docs ?? ""
					completionItems.push(snippetCompletion);
				}
			}
			return completionItems;
		}
	}, ...['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']);
	context.subscriptions.push(language)
}

export function deactivate() { }
